using UnityEngine;

public class BaseView
{
    public GameObject gameObject; // 当前的UI物体
    protected virtual bool RecordPos => false; // 重新打开时会设置为上次关闭时的位置
    public void Init()
    {
        if (RecordPos)
        {
            var hasValue = GameSetting.Instance.settingData.uiPosMap.TryGetValue(this.GetType().Name, out Vector3 pos);
            if (hasValue)
            {
                gameObject.GetComponent<RectTransform>().anchoredPosition = pos;
            }
        }
        OnInit();
    }

    public void Show()
    {
        OnShow();
    }

    public void Hide()
    {
        OnHide();
    }

    public void Dispose()
    {
        if (RecordPos)
        {
            GameSetting.Instance.settingData.uiPosMap[this.GetType().Name] = gameObject.GetComponent<RectTransform>().anchoredPosition;
            GameSetting.Instance.Save();
        }
        OnDispose();
    }

    public void Update(float dt)
    {
        OnUpdate(dt);
    }

    protected virtual void OnInit() {}
    protected virtual void OnShow() {}
    protected virtual void OnHide() {}

    protected virtual void OnDispose() {}

    protected virtual void OnUpdate(float dt) {}

    protected void CloseSelf()
    {
        UIMgr.Instance.Close(this.GetType().Name);
    }
}